//
//  DHS2_SearchRecommendBrandsItemCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/4.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHS2_SearchRecommendBrandsItemCell.h"
#import "DHHomeRecommendCollectionCell.h"
#import "DHS2_ProductDetailController.h"

@interface DHS2_SearchRecommendBrandsItemCell()

@property (assign, nonatomic) BOOL isLiked;
@property (strong, nonatomic) NSDictionary *dic;

@end

@implementation DHS2_SearchRecommendBrandsItemCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"DHHomeRecommendCollectionCell" bundle:nil] forCellWithReuseIdentifier:kDHHomeRecommendCollectionCell];
    
    WeakSelf(weakSelf)
    [self.niceButton bk_addEventHandler:^(id sender) {
        if (weakSelf.isLiked) {
            [weakSelf.niceButton setImage:[UIImage imageNamed:@"product_unlike"] forState:UIControlStateNormal];
            weakSelf.isLiked = !weakSelf.isLiked;
            [[DHNetAPIClient shareDHNetAPIClient] request_brand_collectWithParams:@{@"brand_id":[weakSelf.dic objectForKey:@"_id"],@"collect":[NSNumber numberWithBool:!weakSelf.isLiked]} andBlock:^(id data, NSError *error) {
                
            }];
        }else{
            [weakSelf.niceButton setImage:[UIImage imageNamed:@"product_like"] forState:UIControlStateNormal];
            weakSelf.isLiked = !weakSelf.isLiked;
            [[DHNetAPIClient shareDHNetAPIClient] request_brand_collectWithParams:@{@"brand_id":[weakSelf.dic objectForKey:@"_id"],@"collect":[NSNumber numberWithBool:!weakSelf.isLiked]} andBlock:^(id data, NSError *error) {
                
            }];
        }
    } forControlEvents:UIControlEventTouchUpInside];
}

//- (void)updateCellArr:(NSArray *)arr{
//    self.brandArr = arr;
//}

- (void)updateCell:(NSDictionary *)dic{
    self.dic = dic;
    self.isLiked = [[dic objectForKey:@"collected"] boolValue];
    
    if (self.isLiked) {
        [self.niceButton setImage:[UIImage imageNamed:@"product_like"] forState:UIControlStateNormal];
    }else{
        [self.niceButton setImage:[UIImage imageNamed:@"product_unlike"] forState:UIControlStateNormal];
    }
    
    [self.iconImageView dh_setImageURLString:[dic objectForKey:@"logo"]];
    self.nameLabel.text = [dic objectForKey:@"name"];
    self.productLabel.text = [NSString stringWithFormat:@"%lu个服饰",[[dic objectForKey:@"products"] count]];
    self.productsArr = [dic objectForKey:@"products"];
    [self.collectionView reloadData];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - UICollectionViewDataSource and UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.productsArr.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DHHomeRecommendCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kDHHomeRecommendCollectionCell forIndexPath:indexPath];
    
    NSDictionary *model = [self.productsArr objectAtIndex:indexPath.row];
    [cell updateCell:model];
    return cell;
}

#pragma mark ---- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return (CGSize){(kScreenWidth - 16 - 8)/2,280};
}

// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

// 两个cell之间的最小间距，是由API自动计算的，只有当间距小于该值时，cell会进行换行
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.productsArr objectAtIndex:indexPath.row];
    DHS2_ProductDetailController *vc = [[DHS2_ProductDetailController alloc] initWithProduct:model];
    vc.brandID = [self.dic objectForKey:@"_id"];
    vc.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

@end
